package NetWork.Exercise006;

import java.io.*;
import java.net.Socket;

/**
 * ClassName:Client
 * Package:NetWork.Exercise001
 * Description:
 *
 * @Author:ZhangXin
 * @Create:2023/3/7-22:41
 * @Version:v1.0
 */
public class Client {
    //频繁创建线程并销毁非常浪费系统资源,所以需要用线程池优化
    public static void main(String[] args) throws IOException {
        //创建Socket对象并连接服务器
        Socket socket = new Socket("127.0.0.1", 1002);
        //读取本地文件中的数据并写到服务器中
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("E:\\IDER\\IDEAWorkCode\\IDEADemo\\src\\NetWork\\Exercise003\\clientdir\\a.jpg"));
        BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());
        byte[] bytes = new byte[1024 * 1024];
        int len;
        while ((len = bis.read(bytes)) != -1) {
            bos.write(bytes, 0, len);
        }

        //网服务器写出结束标记
        socket.shutdownOutput();

        //接收服务器的回写数据
        BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        String line = br.readLine();
        System.out.println(line);

        socket.close();


    }
}
